<?php

class Pilot extends MY_SecuredController {

	public function __construct() {
		parent::MY_SecuredController();

		$this->requireRole('User');

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		
		$this->lang->load('home');
		$this->lang->load('profile');
	}

	public function edit($civlid) {
		$this->_preRender($civlid);
        $person = PersonModel::getPerson($civlid);
        
        // check permissions: owners can edit everything, admins only if the person belongs to their club
        if (!$this->_checkAdminClub($person)) {
            return false;
        }
		
		try {
			if (!$this->input->post('action')) {
				//$person = PersonModel::getPerson($civlid);
				
				$data['person'] = $person;
				$data['pilot'] = $person->Pilot;
				
				$this->render('profile/pilot', $data);
			}
			else {
				$p = $person->Pilot;
				$data['pilot'] = $p;

				$p->merge($_POST);
				$p->active = $this->input->post('active')? TRUE : FALSE;
				//$p->replace();

				try {
					$p->replace();
					$this->redirect('profile/pilot/edit/'.$civlid, 'Pilot profile updated successfully!');
				}
				catch (Exception $e) {
					$this->render('profile/pilot', $data, $this->error(array(
						'message' => 'Ooops, the profile could not be updated - please try again later.',
						'exception' => $e
					)));
				}
			}
		}
		catch (Exception $e) {
			echo '<pre>'; echo $e; echo '</pre>';
		}
	}
}
?>
